﻿using Microsoft.Xna.Framework;

namespace XNAssist
{
    /// <summary>
    /// A simple stopwatch that counts up time while it is running
    /// </summary>
    /// <remarks> DON'T FORGET TO CALL THE UPDATE METHOD IN THE GAME LOOP! </remarks>
    public class Stopwatch
    {
        /// <summary>
        /// The time since the stopwatch started running
        /// </summary>
        public float TimeRunning { get; private set; }

        /// <summary>
        /// Whether the stopwatch is running
        /// </summary>
        public bool Running { get; private set; }

        /// <summary>
        /// A simple constructor
        /// </summary>
        public Stopwatch()
        {
            
        }

        /// <summary>
        /// A simple constructor
        /// </summary>
        /// <param name="running"> Whether the stopwatch starts out running or not </param>
        public Stopwatch(bool running)
        {
            Running = running;
        }

        /// <summary>
        /// Updates the stopwatch
        /// </summary>
        /// <param name="gameTime"> The GameTime variable </param>
        public void Update(GameTime gameTime)
        {
            if (!Running) return;
            TimeRunning += (float)gameTime.ElapsedGameTime.TotalSeconds;
        }

        /// <summary>
        /// Starts the stopwatch
        /// </summary>
        public void Start()
        {
            Running = true;
        }

        /// <summary>
        /// Restarts the stopwatch (starts it and sets the time to zero)
        /// </summary>
        public void Restart()
        {
            TimeRunning = 0f;
            Running = true;
        }

        /// <summary>
        /// Pauses the stopwatch
        /// </summary>
        public void Pause()
        {
            Running = false;
        }

        /// <summary>
        /// Stops the stopwatch (pauses it and resets it to zero)
        /// </summary>
        public void Stop()
        {
            Running = false;
            TimeRunning = 0f;
        }

        /// <summary>
        /// Sets the time to zero
        /// </summary>
        public void Reset()
        {
            TimeRunning = 0f;
        }
    }
}
